home *** CD-ROM | disk | FTP | other *** search
- //---------------------------------------------------------------------------
- // Borland C++Builder
- // Copyright (c) 1987, 1998 Borland International Inc. All Rights Reserved.
- //---------------------------------------------------------------------------
- /* Standard C++ library examples */
-
- #include "std1.h"
- #include <algorithm>
- #include <bitset>
- #include <complex>
- #include <ctype.h>
- #include <deque>
- #include <fstream.h>
- #include <functional>
- #include <iomanip.h>
- #include <iostream.h>
- #include <iterator>
- #include <limits>
- #include <list>
- #include <map>
- #include <memory>
- #include <numeric>
- #include <queue>
- #include <set>
- #include <stack>
- #include <stdexcept>
- #include <string.h>
- #include <string>
- #include <strstrea.h>
- #include <utility>
- #include <vector>
-
- using namespace std;
-
- extern int ct; // counter for current memo line
-
- int accum_ex () /* accum */
- {
- //
- // Typedef for vector iterators.
- //
- Form1->Memo1->Lines->Strings[ct++] = " ========== Accumulator Example =========";
- typedef vector<int>::iterator iterator;
- //
- // Initialize a vector using an array of integers.
- //
- int d1[10] = {1,2,3,4,5,6,7,8,9,10};
- vector<int> v1(d1+0, d1+10);
- //
- // Accumulate sums and products.
- //
- int sum = accumulate(v1.begin(), v1.end(), 0);
- //
- // Output the results.
- //
- Form1->Memo1->Lines->Strings[ct++] = "For the series: ";
- for(iterator i = v1.begin(); i != v1.end(); i++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*i) + " ";
- ct++;
- Form1->Memo1->Lines->Strings[ct++] = " where N = 10.";
- Form1->Memo1->Lines->Strings[ct++] = "The sum = (N*N + N)/2 = " +IntToStr(sum);
-
- return 0;
- }
-
-
- int advance_ex () /* advance */
-
- {
- //
- // Initialize a list using an array.
- //
- Form1->Memo1->Lines->Strings[ct++] = " ============= Advance Example ==========";
- int arr[6] = {3,4,5,6,7,8};
- list<int> l(arr+0, arr+6);
- //
- // Declare a list iterator, s.b. a ForwardIterator.
- //
- list<int>::iterator itr = l.begin();
- //
- // Output the original list.
- //
- Form1->Memo1->Lines->Strings[ct++] = "For the list: ";
- for( itr = l.begin(); itr != l.end(); itr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
- ct++;
- Form1->Memo1->Lines->Strings[ct++] = "When the iterator is initialized to l.begin(),";
- Form1->Memo1->Lines->Strings[ct++] = " it points to "+IntToStr(*itr);
- //
- // operator+ is not available for a ForwardIterator, so use advance.
- //
- advance(itr, 4);
- Form1->Memo1->Lines->Strings[ct++] = "After advance(itr,4), the iterator points to "+IntToStr(*itr);
-
- return 0;
- }
-
- int adj_diff_ex () /* adj_diff */
- {
- //
- // Initialize a vector of ints from an array.
- //
- typedef vector<int>::iterator iterator;
- Form1->Memo1->Lines->Strings[ct++] = " ========== Adj_difference Example ======";
- int arr[10] = {1,1,2,3,5,8,13,21,34,55};
- vector<int> v(arr+0, arr+10);
- //
- // Two uninitialized vectors for storing results.
- //
- vector<int> diffs(10);
- //
- // Calculate difference(s) using default operator (minus).
- //
- adjacent_difference(v.begin(),v.end(),diffs.begin());
- //
- // Output the results.
- //
- Form1->Memo1->Lines->Strings[ct++] = "For the vector: " ;
- for(iterator itr = v.begin(); itr != v.end(); itr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
- ct++;
- Form1->Memo1->Lines->Strings[ct++] = "The differences between adjacent elements are: ";
- for(iterator itr = diffs.begin(); itr != diffs.end(); itr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
- ct++;
- return 0;
- }
-
- // *** start alg1 source
-
- class iotaGen {
- public:
- iotaGen (int iv) : current(iv) { }
- operator () () { return current++; }
- private:
- int current;
- };
-
- void fill_example ()
- // illustrate the use of the fill and fill_n functions
- {
- Form1->Memo1->Lines->Strings[ct++] = "Illustrating fill function:" ;
- // example 1, fill an array with initial values
- char buffer[100], *bufferp = buffer;
- fill(bufferp, bufferp + 100, '\0');
- fill_n(bufferp, 10, 'x');
- Form1->Memo1->Lines->Strings[ct++] = buffer ;
-
- // example 2, use fill to initialize a list
- list<string> aList;
- list<string> ResultList;
- list<string>::iterator listItr;
-
- fill_n (inserter(aList, aList.begin()), 10, "empty");
- copy(aList.begin(), aList.end(), inserter(ResultList,ResultList.begin()));
- for(listItr = ResultList.begin(); listItr != ResultList.end(); listItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + (String)(*listItr).data() + " ";
- ct++;
-
- // example 3, use fill to overwrite values in a list
- fill (aList.begin(), aList.end(), "full");
- for(listItr = aList.begin(); listItr != aList.end(); listItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + (String)(*listItr).data() + " ";
- ct++;
-
- // example 4, fill in a portion of a list
- vector<int> iVec(10);
- vector<int>::iterator vItr;
- generate (iVec.begin(), iVec.end(), iotaGen(1));
- vector<int>::iterator seven = find(iVec.begin(), iVec.end(), 7);
- fill(iVec.begin(), seven, 0);
- for(vItr = iVec.begin(); vItr != iVec.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- }
-
- void copy_example()
- // illustrate the use of the copy function
- {
- Form1->Memo1->Lines->Strings[ct++] = "Illustrating copy function: " ;
- char * source = "reprise";
- char * surpass = "surpass";
- char buffer[120], *bufferp = buffer;
- vector<char>::iterator listItr;
-
- // example 1, a simple copy
- copy(source, source + strlen(source) + 1, bufferp);
-
- // example 2, self copies
- * copy(bufferp + 2, bufferp+ strlen(buffer), bufferp) = '\0';
- int buflen = strlen(buffer) + 1;
- copy_backward(bufferp, bufferp + buflen, bufferp + buflen + 3);
- copy(surpass, surpass + 3, bufferp);
-
- for(listItr = bufferp; listItr < bufferp + strlen(buffer); listItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + *listItr;
- ct++;
-
- // example 4, use copy to convert type
- list<char> char_list;
- list<char>::iterator listItr2;
- copy(bufferp, bufferp + strlen(buffer),inserter(char_list,char_list.end()));
- char * big = "big ";
- copy(big, big + 4, inserter(char_list, char_list.begin()));
- for(listItr2 = char_list.begin(); listItr2 != char_list.end(); listItr2++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + *listItr2;
- ct++;
-
- char /*buffer2[200],*/ *buffer2p = buffer;
- * copy(char_list.begin(), char_list.end(), buffer2p) = '\0';
- Form1->Memo1->Lines->Strings[ct++] = buffer2p ;
- }
-
- # include <strstrea.h>
-
- string generateLabel() {
- // generate a label string of the form L_ddd
- static int lastLabel = 0;
- char labelBuffer[80];
- ostrstream ost(labelBuffer, 80);
- ost << "L_" << lastLabel++ << '\0';
- return string(labelBuffer);
- }
-
- void generate_example ()
- // illustrate the use of the generate and genrate_n functions
- {
- Form1->Memo1->Lines->Strings[ct++] = "Illustrating generate algorithm:" ;
-
- // example 1, generate a list of label numbers
- list<string> labelList;
- list<string>::iterator listItr;
- generate_n (inserter(labelList, labelList.begin()), 4, generateLabel);
- for(listItr = labelList.begin(); listItr != labelList.end(); listItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + (String)(*listItr).data() + " ";
- ct++;
-
- // example 2, generate an arithmetic progression
- vector<int> iVec(10);
- vector<int>::iterator vItr;
- generate (iVec.begin(), iVec.end(), iotaGen(2));
- generate_n (iVec.begin(), 5, iotaGen(7));
- for(vItr = iVec.begin(); vItr != iVec.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- }
-
- void swap_example ()
- // illustrate the use of the algorithm swap_ranges
- {
- Form1->Memo1->Lines->Strings[ct++] = "Illustrating swap_ranges algorithm:" ;
-
- // first make two parallel sequences
- int data[] = {12, 27, 14, 64}, *datap = data;
- vector<int> aVec(4);
- vector<int>::iterator vItr;
- generate (aVec.begin(), aVec.end(), iotaGen(1));
-
- // illustrate swap and swap_itr
- swap(data[0], data[2]);
- for(vItr = data; vItr != data+4; vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- vector<int>::iterator last = aVec.end(); last--;
- iter_swap(aVec.begin(), last);
- for(vItr = aVec.begin(); vItr != aVec.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
-
- // now swap the entire sequence
- swap_ranges (aVec.begin(), aVec.end(), datap);
- for(vItr = data; vItr != data+4; vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- for(vItr = aVec.begin(); vItr != aVec.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- }
-
- int alg1_ex() /* alg1 */
- {
- Form1->Memo1->Lines->Strings[ct++] = "===== STL generic algorithms -- initialization ";
- fill_example();
- copy_example();
- generate_example();
- swap_example();
-
- Form1->Memo1->Lines->Strings[ct++] = "End of initialization tests" ;
- return 0;
- }
-
-
- // *** start alg2 source
-
- int randomInteger(int m)
- { return rand() % m; }
-
- bool isLeapYear (unsigned int year)
- {
- if (year % 1000 == 0)
- return true;
- if (year % 100 == 0)
- return false;
- if (year % 4 == 0)
- return true;
- return false;
- }
-
- bool operator < (const string & left, const string & right)
- { return left.compare(right) < 0; }
-
- void split
- (const string & text, const string & separators, list<string> & words)
- {
- int n = text.length();
- int start, stop;
-
- start = text.find_first_not_of(separators);
- while ((start >= 0) && (start < n)) {
- stop = text.find_first_of(separators, start);
- if ((stop < 0) || (stop > n)) stop = n;
- words.push_back (text.substr(start, stop-start));
- start = text.find_first_not_of(separators, stop+1);
- }
- }
-
-
- void find_test()
- // illustrate use of the find function
- {
- Form1->Memo1->Lines->Strings[ct++] = "Test of algorithm find:" ;
- int vintageYears[] = {1967, 1972, 1974, 1980, 1995};
-
- vector<int>::iterator start = vintageYears;
- vector<int>::iterator stop = vintageYears + 5;
-
- vector<int>::iterator where = find_if(start, stop, isLeapYear);
-
- if (where != stop)
- Form1->Memo1->Lines->Strings[ct++] = "first vintage leap year is " + IntToStr(*where) ;
- else
- Form1->Memo1->Lines->Strings[ct++] = "no vintage leap years" ;
-
- where = find(start, stop, 1995);
-
- if (where != stop)
- Form1->Memo1->Lines->Strings[ct++] = "1995 is position " + IntToStr(where - start) + " in sequence" ;
- else
- Form1->Memo1->Lines->Strings[ct++] = "1995 does not occur in sequence" ;
-
- }
-
- void find_adjacent_test()
- {
- Form1->Memo1->Lines->Strings[ct++] = "Test of algorithm find adjacent" ;
- char * text = "The bookkeeper carefully opened the door";
-
- vector<char>::iterator start = text;
- vector<char>::iterator stop = text + strlen(text);
-
- vector<char>::iterator where = start;
-
- Form1->Memo1->Lines->Strings[ct++] = "In the text " + (String)text ;
- while ((where = adjacent_find(where, stop)) != stop) {
- Form1->Memo1->Lines->Strings[ct++] = "double " + IntToStr(*where) + " in position " + IntToStr(where - start) ;
- ++where;
- }
- }
-
- void search_test()
- // illustrate the use of the search function
- {
- Form1->Memo1->Lines->Strings[ct++] = "Test of algorithm search:" ;
- char * base = "aspirations";
- char * text = "ration";
-
- char * where = search(base, base + strlen(base), text, text + strlen(text));
-
- if (*where != '\0')
- Form1->Memo1->Lines->Strings[ct++] = "substring begins in position " << IntToStr(where - base) ;
- else
- Form1->Memo1->Lines->Strings[ct++] = "substring does not occur in text" ;
- }
-
- void max_min_example ()
- // illustrate use of max_element and min_element algorithms
- {
- string MyString;
-
- Form1->Memo1->Lines->Strings[ct++] = "Test of max and min algorithms: " ;
- // make a vector of random numbers between 0 and 99
- vector<int> numbers(25);
- for (int i = 0; i < 25; i++)
- numbers[i] = randomInteger(100);
-
- // print the maximum
- vector<int>::iterator max =
- max_element(numbers.begin(), numbers.end());
- Form1->Memo1->Lines->Strings[ct++] = "largest value was " + IntToStr(*max) ;
-
- // example using strings
- string text =
- "it was the best of times, it was the worst of times.";
- list<string>words;
- split(text, " .,!:;", words);
- MyString = (* min_element(words.begin(), words.end()));
- Form1->Memo1->Lines->Strings[ct] = "The smallest word is ";
- Form1->Memo1->Lines->Strings[ct] = Form1->Memo1->Lines->Strings[ct]+(String)MyString.data();
- ct++;
- MyString = (* max_element(words.begin(), words.end()));
- Form1->Memo1->Lines->Strings[ct++] = "and the largest word is "+(String)MyString.data();
- }
-
- void mismatch_test(char * a, char * b)
- // illustrate the use of the mismatch function
- {
- pair<char *, char *> differPositions(0, 0);
- char * aDiffPos;
- char * bDiffPos;
-
- if (strlen(a) < strlen(b)) {
- differPositions = mismatch(a, a + strlen(a), b);
- aDiffPos = differPositions.first;
- bDiffPos = differPositions.second;
- }
- else {
- differPositions = mismatch(b, b + strlen(b), a);
- aDiffPos = differPositions.second;
- bDiffPos = differPositions.first;
- }
-
- Form1->Memo1->Lines->Strings[ct++] = "string " + (String)a;
- if (*aDiffPos == *bDiffPos)
- Form1->Memo1->Lines->Strings[ct] = " is equal to ";
- else if (*aDiffPos < *bDiffPos)
- Form1->Memo1->Lines->Strings[ct] = " is less than ";
- else
- Form1->Memo1->Lines->Strings[ct] = " is greater than ";
- Form1->Memo1->Lines->Strings[ct] = Form1->Memo1->Lines->Strings[ct] + String(b) ;
- ct++;
- }
-
- int alg2_ex() /* alg2 */
- {
- Form1->Memo1->Lines->Strings[ct++] = "===== STL generic algorithms -- Searching " ;
- find_test();
- find_adjacent_test();
- search_test;
- max_min_example();
- mismatch_test("goody", "goody");
- mismatch_test("good", "goody");
- mismatch_test("goody", "good");
- mismatch_test("good", "fred");
- mismatch_test("fred", "good");
-
- Form1->Memo1->Lines->Strings[ct++] = "End of search algorithms test program" ;
- return 0;
- }
-
-
- // *** start alg3 source
-
- class iotaGenerator {
- public:
- iotaGenerator (int iv) : current(iv) { }
- operator () () { return current++; }
- private:
- int current;
- };
-
- bool isEven(int n) { return 0 == (n % 2); }
-
- void reverse_example ()
- // illustrate the use of the reverse function
- {
- Form1->Memo1->Lines->Strings[ct++] = "Illustrating reverse algorithm:" ;
-
- // example 1, reversing a string
- char * text = "Rats live on no evil star";
- reverse (text, text + strlen(text));
- Form1->Memo1->Lines->Strings[ct++] = text ;
-
- // example 2, reversing a list
- list<int> iList;
- list<int>::iterator lItr;
- generate_n(inserter(iList, iList.begin()), 10, iotaGenerator(2));
- reverse (iList.begin(), iList.end());
- for(lItr = iList.begin(); lItr != iList.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
- ct++;
- }
-
- void replace_example ()
- // illustrate the use of the replace function
- {
- Form1->Memo1->Lines->Strings[ct++] = "Illustrating replace algorithm:" ;
-
- // make vector 0 1 2 3 4
- vector<int> numbers(11);
- vector<int>::iterator vItr;
- for (int i = 0; i < 11; i++)
- numbers[i] = i < 5 ? i : 10 - i;
- for(vItr = numbers.begin(); vItr != numbers.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
-
-
- // replace 0 by 2
- replace (numbers.begin(), numbers.end(), 3, 7);
- for(vItr = numbers.begin(); vItr != numbers.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
-
- // replace even numbers by 9
- replace_if (numbers.begin(), numbers.end(), isEven, 9);
- for(vItr = numbers.begin(); vItr != numbers.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
-
- // copy into a list, replacing 9 by 3
- int aList[] = {2, 1, 4, 3, 2, 5};
- int bList[6];
- int cList[6];
- replace_copy (aList, aList+6, &bList[0], 2, 7);
- replace_copy_if (bList, bList+6, &cList[0], bind2nd(greater<int>(), 3), 8);
- for(vItr = bList; vItr < bList+6; vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- for(vItr = cList; vItr < cList+6; vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- }
-
- void rotate_example ()
- // illustrate the use of the rotate function
- {
- Form1->Memo1->Lines->Strings[ct++] = "Illustrating rotate algorithm:" ;
-
- // create the list 1 2 3 ... 10
- list<int> iList;
- list<int>::iterator lItr;
- generate_n(inserter(iList, iList.begin()), 10, iotaGenerator(1));
-
- // find the location of the seven
- list<int>::iterator middle = find(iList.begin(), iList.end(), 7);
-
- // now rotate around that location
- rotate(iList.begin(), middle, iList.end());
- for(lItr = iList.begin(); lItr != iList.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
- ct++;
-
- // rotate again around the same location
- list<int> cList;
- rotate_copy(iList.begin(), middle, iList.end(),
- inserter(cList, cList.begin()));
- for(lItr = cList.begin(); lItr != cList.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
- ct++;
- }
-
-
- void partition_example ()
- // illustrate the use of the paration function
- {
- Form1->Memo1->Lines->Strings[ct++] = "Illustrating partition algorithm:" ;
-
- // first make the vector 1 2 3 ... 10
- vector<int> numbers(10);
- vector<int>::iterator vItr;
- generate(numbers.begin(), numbers.end(), iotaGenerator(1));
-
- // now put the odd values low, even values high
- vector<int>::iterator result = partition (numbers.begin(), numbers.end(), isEven);
- for(vItr = numbers.begin(); vItr < numbers.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- Form1->Memo1->Lines->Strings[ct++] = "middle location " + IntToStr(result - numbers.begin()) ;
-
- // now do a stable partition
- generate(numbers.begin(), numbers.end(), iotaGenerator(1));
- for(vItr = numbers.begin(); vItr < numbers.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- }
-
-
- bool nameCompare(char * a, char * b) { return strcmp(a, b) <= 0; }
-
- void permutation_example ()
- // illustrate the use of the next_permutation function
- {
- // start with the values 1 2 3 in sequence
- int start [] = {1, 2, 3 };
- vector<int>::iterator vItr;
- Form1->Memo1->Lines->Strings[ct++] = "Illustrating permutation: " ;
- do
- {
- for(vItr = start; vItr < start+3; vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- }
- while (next_permutation(start, start + 3));
-
- char * names[] = {"Alpha", "Beta", "Gamma"};
- vector<char *>::iterator cItr;
- do
- {
- for(cItr = names; cItr < names + 3; cItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + *cItr + " ";
- ct++;
- }
- while (next_permutation(names, names + 3, nameCompare));
-
- char * word = "bela";
- do
- Form1->Memo1->Lines->Strings[ct] = Form1->Memo1->Lines->Strings[ct]+ word + " ";
- while (prev_permutation(word, &word[4]));
- ct++;
- }
-
- void inplace_merge_example ()
- // illustrate the use of the inplace_merge function
- {
- Form1->Memo1->Lines->Strings[ct++] = "Illustrating inplace merge algorithm" ;
-
- // first generate the numbers 0 2 4 6 8 1 3 5 7 9
- vector<int> numbers(10);
- vector<int>::iterator vItr;
- for (int i = 0; i < 10; i++)
- numbers[i] = i < 5 ? 2 * i : 2 * (i - 5) + 1;
- for(vItr = numbers.begin(); vItr < numbers.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- vector<int>::iterator midvec = find(numbers.begin(), numbers.end(), 1);
-
- // copy them into a list
- list<int> numList;
- list<int>::iterator lItr;
- copy(numbers.begin(), numbers.end(),
- inserter(numList, numList.begin()));
- list<int>::iterator midList = find(numList.begin(), numList.end(), 1);
- for(lItr = numList.begin(); lItr != numList.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
- ct++;
-
- // now put them back together
- inplace_merge(numbers.begin(), midvec, numbers.end());
- inplace_merge(numList.begin(), midList, numList.end());
- for(lItr = numList.begin(); lItr != numList.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
- ct++;
- }
-
- struct RandomInteger
- {
- operator() (int m) { return rand() % m; }
- };
-
- void random_shuffle_example()
- // illustrate the use of the random_shuffle function
- {
- // first make the vector 1 2 3 ... 10
- vector<int> numbers(10);
- vector<int>::iterator vItr;
- Form1->Memo1->Lines->Strings[ct++] = "Illustrating random shuffle:" ;
- generate(numbers.begin(), numbers.end(), iotaGenerator(1));
-
- RandomInteger random;
-
- // randomly shuffle the elements
- random_shuffle(numbers.begin(), numbers.end(), random);
- for(vItr = numbers.begin(); vItr < numbers.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
-
- // do it again
- random_shuffle(numbers.begin(), numbers.end(), random);
- for(vItr = numbers.begin(); vItr < numbers.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- }
-
- int alg3_ex() /* alg3 */
- {
- Form1->Memo1->Lines->Strings[ct++] = "===== STL generic algorithms -- in-place " ;
-
- reverse_example();
- replace_example();
- rotate_example();
- partition_example();
- permutation_example();
- inplace_merge_example();
- random_shuffle_example();
-
- Form1->Memo1->Lines->Strings[ct++] = "End of in-place algorithm sample program" ;
-
- return 0;
- }
-
- // *** start alg4 source
-
-
- void remove_example ()
- // illustrate the use of the remove algorithm
- {
- Form1->Memo1->Lines->Strings[ct++] = "Remove Algorithm examples:" ;
-
- // create a list of numbers
- int data[] = {1, 2, 4, 3, 1, 4, 2};
- list<int> aList;
- list<int>::iterator itr = aList.begin();
- copy (data, data+7, inserter(aList, aList.begin()));
- Form1->Memo1->Lines->Strings[ct++] = "Original list: ";
- for(itr = aList.begin(); itr != aList.end(); itr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
-
- // remove 2's, copy into a new list
- ct++;
- list<int> newList;
- remove_copy (aList.begin(), aList.end(), back_inserter(newList), 2);
- Form1->Memo1->Lines->Strings[ct++] = "After removing 2's: ";
- for(itr = newList.begin(); itr != newList.end(); itr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
- ct++;
- // remove 2's in place
- list<int>::iterator where;
- where = remove(aList.begin(), aList.end(), 2);
- Form1->Memo1->Lines->Strings[ct++] = "List after removal, before erase: ";
- for(itr = aList.begin(); itr != aList.end(); itr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
- ct++;
- aList.erase(where, aList.end());
- Form1->Memo1->Lines->Strings[ct++] = "List after erase: ";
- for(itr = aList.begin(); itr != aList.end(); itr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
- ct++;
-
- // remove all even values
- where = remove_if (aList.begin(), aList.end(), isEven);
- aList.erase(where, aList.end());
- Form1->Memo1->Lines->Strings[ct++] = "List after removing even values: ";
- for(itr = aList.begin(); itr != aList.end(); itr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
- ct++;
-
- }
-
- void unique_example ()
- // illustrate use of the unqiue algorithm
- {
- // first make a list of values
- int data[] = {1, 3, 3, 2, 2, 4};
- list<int> aList;
- Form1->Memo1->Lines->Strings[ct++] = "Illustrating unique algorithm" ;
- copy(data, data+6, inserter(aList, aList.begin()));
- list<int>::iterator itr = aList.begin();
- Form1->Memo1->Lines->Strings[ct++] = "Original List: ";
- for(itr = aList.begin(); itr != aList.end(); itr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
- ct++;
-
- // copy unique elements into a set
- set<int, less<int> > aSet;
- set<int, less<int> >::iterator s_itr = aSet.begin();
- unique_copy(aList.begin(), aList.end(), inserter(aSet, aSet.begin()));
- Form1->Memo1->Lines->Strings[ct++] = "Set after unique_copy: ";
- for(s_itr = aSet.begin(); s_itr != aSet.end(); s_itr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*s_itr) + " ";
- ct++;
-
- // copy unique elements in place
- list<int>::iterator where;
- where = unique(aList.begin(), aList.end());
- Form1->Memo1->Lines->Strings[ct++] = "List after calling unique: ";
- for(itr = aList.begin(); itr != aList.end(); itr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
- ct++;
-
- // remove trailing values
- aList.erase(where, aList.end());
- Form1->Memo1->Lines->Strings[ct++] = "List after erase: ";
- for(itr = aList.begin(); itr != aList.end(); itr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
- ct++;
- }
-
- int alg4_ex() { /* alg4 */
- Form1->Memo1->Lines->Strings[ct++] = "===== STL generic algorithms -- Removal " ;
- remove_example();
- unique_example();
-
- cout << "End of removal algorithms sample program" ;
- return 0;
- }
-
- // *** start alg5 source
-
- // prototypes used to halt warnings
- bool isVowel (char);
- void count_example();
- void accumulate_example();
- template<class T>
- list<T> & listadd(list<T> & base, T & newValue);
- void inner_product_example();
- void equal_example();
-
-
- bool isVowel (char c)
- {
- switch (c) {
- case 'a': case 'A':
- case 'e': case 'E':
- case 'i': case 'I':
- case 'o': case 'O':
- case 'u': case 'U':
- return true;
- }
- return false;
- }
-
- void count_example()
- // illustrate the use of the count function
- {
- int ecount = 0;
- int vowelCount = 0;
-
- Form1->Memo1->Lines->Strings[ct++] = "Illustrating count function:" ;
- char * text = "Now is the time to begin";
-
- count (text, text + strlen(text), 'e', ecount);
- count_if (text, text + strlen(text), isVowel, vowelCount);
-
- Form1->Memo1->Lines->Strings[ct++] = "There are " + IntToStr(ecount) + " letter e's " + "and "
- + IntToStr(vowelCount) + " vowels in the text:" + text ;
- }
-
- list<int>& intReplicate (list<int>& nums, int n)
- // add n to 1 to list
- {
- while (n) nums.push_back(n--);
- return nums;
- }
-
- void accumulate_example()
- // illustrate the use of the accumulate function
- {
- int numbers[] = {1, 2, 3, 4, 5};
-
- int sum = accumulate(numbers, numbers+5, 0);
-
- Form1->Memo1->Lines->Strings[ct++] = "Illustrating accumulate function:" ;
- Form1->Memo1->Lines->Strings[ct++] = "The sum of the first five numbers is " + IntToStr(sum) ;
-
- // example with different types for init
- list<int> nums;
- list<int>::iterator itr = nums.begin();
- nums = accumulate(numbers, numbers+5, nums, intReplicate);
- for(itr = nums.begin(); itr != nums.end(); itr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
- ct++;
- }
-
- void inner_product_example()
- // illustrate the use of the inner_product function
- {
- int a[] = {4, 3, -2};
- int b[] = {7, 3, 2};
-
- // example 1, simple inner product
- Form1->Memo1->Lines->Strings[ct++] = "Illustrating inner product:" ;
- int in1 = inner_product(a, a+3, b, 0);
- Form1->Memo1->Lines->Strings[ct++] = "Inner product is " + IntToStr(in1) ;
-
- // example 2, using different operations
- bool anyequal = inner_product(a, a+3, b, true, logical_or<bool>(), equal_to<int>());
- Form1->Memo1->Lines->Strings[ct++] = "any equal? " + IntToStr(anyequal) ;
- }
-
- void equal_example()
- // illustrate the use of the equal function
- {
- int a[] = {4, 5, 3};
- int b[] = {4, 3, 3};
- int c[] = {4, 5, 3};
-
- Form1->Memo1->Lines->Strings[ct++] = "Illustrating equal function:" ;
- Form1->Memo1->Lines->Strings[ct++] = "a = b is:" + IntToStr(equal(a, a+3, b)) ;
- Form1->Memo1->Lines->Strings[ct++] = "a = c is:" + IntToStr(equal(a, a+3, c)) ;
- Form1->Memo1->Lines->Strings[ct++] = "a pair-wise-greater_equal b is" + IntToStr(equal(a, a+3, b, greater_equal<int>()));
- }
-
- void lexical_comparison_example ()
- // illustrate the use of the lexical_comparison function
- {
- char * wordOne = "everything";
- char * wordTwo = "everybody";
-
- Form1->Memo1->Lines->Strings[ct++] = "Illustrating lexical_comparison function:" ;
- Form1->Memo1->Lines->Strings[ct++] = "compare everybody to everything " +
- IntToStr(lexicographical_compare(wordTwo, wordTwo + strlen(wordTwo), wordOne, wordOne + strlen(wordOne)));
-
- int a[] = {3, 4, 5, 2};
- int b[] = {3, 4, 5};
- int c[] = {3, 5};
-
- Form1->Memo1->Lines->Strings[ct++] = "compare a to b: " + IntToStr(lexicographical_compare(a, a+4, b, b+3) );
- Form1->Memo1->Lines->Strings[ct++] = "compare a to c: " + IntToStr(lexicographical_compare(a, a+4, c, c+2) );
- }
-
- int alg5_ex() { /* alg5 */
- Form1->Memo1->Lines->Strings[ct++] = "===== STL generic algorithms -- algorithms with scalar results" ;
- count_example();
- accumulate_example();
- inner_product_example();
- equal_example();
- lexical_comparison_example();
- ct++;
- Form1->Memo1->Lines->Strings[ct++] = "End of scalar algorithms test" ;
- return 0;
- }
-
- // *** start alg6 source
-
- int square(int n) { return n * n; }
-
- void transform_example ()
- // illustrate the use of the transform algorithm
- {
- // generate a list of values from 1 to 6
- list<int> aList;
- generate_n (inserter(aList, aList.begin()), 6, iotaGen(1));
- list<int>::iterator itr = aList.begin();
- Form1->Memo1->Lines->Strings[ct++] = "Original list: ";
- for(itr = aList.begin(); itr != aList.end(); itr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
- ct++;
- // transform elements by squaring, copy into vector
- vector<int> aVec(6);
- transform (aList.begin(), aList.end(), aVec.begin(), square);
- vector<int>::iterator vitr;
- Form1->Memo1->Lines->Strings[ct++] = "After squaring: ";
- for(vitr = aVec.begin(); vitr != aVec.end(); vitr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vitr) + " ";
- ct++;
-
- // transform vector again, in place, yielding 4th powers
- transform (aVec.begin(), aVec.end(), aVec.begin(), square);
- Form1->Memo1->Lines->Strings[ct++] = "After squaring again: ";
- for(vitr = aVec.begin(); vitr != aVec.end(); vitr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vitr) + " ";
- ct++;
-
- // transform in parallel, yielding cubes
- vector<int> cubes(6);
- transform (aVec.begin(), aVec.end(), aList.begin(),
- cubes.begin(), divides<int>());
- Form1->Memo1->Lines->Strings[ct++] = "After division: ";
- for(vitr = cubes.begin(); vitr != cubes.end(); vitr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vitr) + " ";
- ct++;
-
- }
-
-
-
- void partial_sum_example ()
- // illustrate the use of the partial sum algorithm
- {
- // generate values 1 to 5
- vector<int> aVec(5);
- vector<int> ResultVec(5);
- generate (aVec.begin(), aVec.end(), iotaGen(1));
- vector<int>::iterator vitr;
-
- // output partial sums
- Form1->Memo1->Lines->Strings[ct++] = "Partial sums examples" ;
- Form1->Memo1->Lines->Strings[ct++] = "Partial sums : ";
- partial_sum (aVec.begin(), aVec.end(), ResultVec.begin()) ;
- for(vitr = ResultVec.begin(); vitr != ResultVec.end(); vitr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vitr) + " ";
- ct++;
- }
-
- void adjacent_difference_example ()
- // illustrate the use of the adjacent difference algorithm
- {
- // generate values 1 to 5
- vector<int> aVec(5);
- vector<int> ResultVec(5);
- generate (aVec.begin(), aVec.end(), iotaGen(1));
-
- // output partial sums
- Form1->Memo1->Lines->Strings[ct++] = "Adjacent Differences examples" ;
- Form1->Memo1->Lines->Strings[ct++] = "Adjacent Differences : ";
- adjacent_difference (aVec.begin(), aVec.end(),ResultVec.begin()) ;
- vector<int>::iterator vitr;
- for(vitr = ResultVec.begin(); vitr != ResultVec.end(); vitr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vitr) + " ";
- ct++;
- //ResultVec.erase(ResultVec.begin(),ResultVec.end());
-
- // output partial products
- Form1->Memo1->Lines->Strings[ct++] = "Adjacent sums: ";
- adjacent_difference (aVec.begin(), aVec.end(),ResultVec.begin(),plus<int>()) ;
- for(vitr = ResultVec.begin(); vitr != ResultVec.end(); vitr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vitr) + " ";
- ct++;
- }
-
-
- int alg6_ex() { /* alg6 */
- Form1->Memo1->Lines->Strings[ct++] = "===== STL generic algorithms -- that transform sequences" ;
-
- transform_example();
- partial_sum_example();
- adjacent_difference_example ();
-
- Form1->Memo1->Lines->Strings[ct++] = "End generic transform algorithms example" ;
- return 0;
- }
-
- // *** start alg7 source
-
- int randomInteger(unsigned int n)
- { return rand() % n; }
-
- int randomValue() { return randomInteger(100); }
-
- void sortExample() {
- Form1->Memo1->Lines->Strings[ct++] = "Sort algorithms" ;
-
- // fill both a vector and a deque
- // with random integers
- vector<int> aVec(15);
- vector<int>::iterator vItr;
- deque<int> aDec(15);
- deque<int>::iterator dItr;
- generate (aVec.begin(), aVec.end(), randomValue);
- generate (aDec.begin(), aDec.end(), randomValue);
- Form1->Memo1->Lines->Strings[ct++] = "Original vectors:";
- for(vItr = aVec.begin(); vItr != aVec.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- for(dItr = aDec.begin(); dItr != aDec.end(); dItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*dItr) + " ";
- ct++;
- Form1->Memo1->Lines->Strings[ct++] = "-----------------------------------";
-
- // sort the vector ascending
- sort (aVec.begin(), aVec.end());
- for(vItr = aVec.begin(); vItr != aVec.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
-
- // sort the deque descending
- sort (aDec.begin(), aDec.end(), greater<int>() );
- for(dItr = aDec.begin(); dItr != aDec.end(); dItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*dItr) + " ";
- ct++;
-
- // sort the vector descending?
- sort (aVec.rbegin(), aVec.rend());
- for(vItr = aVec.begin(); vItr != aVec.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
-
- }
-
- void partial_sort_example() {
- Form1->Memo1->Lines->Strings[ct++] = "Partial sort examples" ;
-
- // make a vector of 15 random integers
- vector<int> aVec(15);
- vector<int>::iterator vItr;
- generate (aVec.begin(), aVec.end(), randomValue);
- for(vItr = aVec.begin(); vItr != aVec.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
-
-
- // partial sort the first seven positions
- partial_sort (aVec.begin(), aVec.begin() + 7, aVec.end());
- for(vItr = aVec.begin(); vItr != aVec.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
-
- // make a list of random integers
- list<int> aList(15, 0);
- list<int>::iterator lItr;
- generate (aList.begin(), aList.end(), randomValue);
- for(lItr = aList.begin(); lItr != aList.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
- ct++;
-
- // sort only the first seven elements
- vector<int> start(7);
- partial_sort_copy (aList.begin(), aList.end(),
- start.begin(), start.end(), greater<int>());
- for(vItr = start.begin(); vItr != start.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- }
-
- void nth_element_example()
- // illustrate the use of the nth_largest function
- {
- Form1->Memo1->Lines->Strings[ct++] = "Nth largest example" ;
-
- // make a vector of random integers
- vector<int> aVec(10);
- generate (aVec.begin(), aVec.end(), randomValue);
-
- // now find the 5th largest
- vector<int>::iterator nth = aVec.begin() + 4;
- nth_element(aVec.begin(), nth, aVec.end());
-
- Form1->Memo1->Lines->Strings[ct++] = "fifth largest is " + IntToStr(*nth) + " in" ;
- for(nth = aVec.begin(); nth != aVec.end(); nth++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*nth) + " ";
- ct++;
-
-
- }
-
- void binary_search_example ()
- // illustrate the use of the binary search functions
- {
- Form1->Memo1->Lines->Strings[ct++] = "Binary search example" ;
-
- // make an ordered vector of 15 random integers
- vector<int> aVec(15);
- vector<int>::iterator vItr;
- generate (aVec.begin(), aVec.end(), randomValue);
- sort (aVec.begin(), aVec.end());
- for(vItr = aVec.begin(); vItr != aVec.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
-
- // see if it contains an eleven
- if (binary_search(aVec.begin(), aVec.end(), 11))
- Form1->Memo1->Lines->Strings[ct++] = "contains an 11" ;
- else
- Form1->Memo1->Lines->Strings[ct++] = "does not contain an 11" ;
-
- // insert an 11 and a 14
- vector<int>::iterator where;
- where = lower_bound (aVec.begin(), aVec.end(), 11);
- aVec.insert (where, 11);
-
- where = upper_bound (aVec.begin(), aVec.end(), 14);
- aVec.insert (where, 14);
- for(vItr = aVec.begin(); vItr != aVec.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
-
- }
-
- void merge_example ()
- // illustrate the use of the merge function
- {
- Form1->Memo1->Lines->Strings[ct++] = "Merge algorithm examples" ;
-
- // make a list and vector of 10 random integers
- vector<int> aVec(10);
- list<int> aList(10, 0);
- list<int>::iterator lItr;
- vector<int>::iterator vItr;
-
- generate (aVec.begin(), aVec.end(), randomValue);
- sort (aVec.begin(), aVec.end());
- generate_n (aList.begin(), 10, randomValue);
- aList.sort();
- Form1->Memo1->Lines->Strings[ct++] = "Merge lists:" ;
-
- for(vItr = aVec.begin(); vItr != aVec.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- for(lItr = aList.begin(); lItr != aList.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
- ct++;
- // merge into a vector
- vector<int> vResult (aVec.size() + aList.size());
- Form1->Memo1->Lines->Strings[ct++] = "Into a vector:" ;
- merge (aVec.begin(), aVec.end(), aList.begin(), aList.end(),
- vResult.begin());
- for(vItr = vResult.begin(); vItr != vResult.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
-
- // merge into a list
- list<int> lResult;
- Form1->Memo1->Lines->Strings[ct++] = "Then a list:" ;
- merge (aVec.begin(), aVec.end(), aList.begin(), aList.end(),
- inserter(lResult, lResult.begin()));
- for(lItr = lResult.begin(); lItr != lResult.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
- ct++;
-
- }
-
- void set_example ()
- // illustrate the use of the generic set functions
- {
- Form1->Memo1->Lines->Strings[ct++] = "Set operations:" ;
-
- // make a couple of ordered lists
- list <int> listOne, listTwo;
- list<int>::iterator lItr;
- generate_n (inserter(listOne, listOne.begin()), 5, iotaGen(1));
- generate_n (inserter(listTwo, listTwo.begin()), 5, iotaGen(3));
-
- Form1->Memo1->Lines->Strings[ct++] = "Original lists:" ;
- for(lItr = listOne.begin(); lItr != listOne.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
- ct++;
- for(lItr = listTwo.begin(); lItr != listTwo.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
- ct++;
- // now do the set operations
- // union - 1 2 3 4 5 6 7
- Form1->Memo1->Lines->Strings[ct++] = "Union:" ;
- list<int> lResult;
- set_union (listOne.begin(), listOne.end(),
- listTwo.begin(), listTwo.end(), inserter(lResult, lResult.begin()));
- for(lItr = lResult.begin(); lItr != lResult.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
- ct++;
- lResult.erase(lResult.begin(),lResult.end());
- // merge - 1 2 3 3 4 4 5 5 6 7
- Form1->Memo1->Lines->Strings[ct++] = "Merge:" ;
- merge (listOne.begin(), listOne.end(),
- listTwo.begin(), listTwo.end(), inserter(lResult, lResult.begin()));
- for(lItr = lResult.begin(); lItr != lResult.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
- ct++;
- lResult.erase(lResult.begin(),lResult.end());
- // intersection 3 4 5
- Form1->Memo1->Lines->Strings[ct++] = "Intersection:" ;
- set_intersection (listOne.begin(), listOne.end(),
- listTwo.begin(), listTwo.end(),inserter(lResult, lResult.begin()));
- for(lItr = lResult.begin(); lItr != lResult.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
- ct++;
- lResult.erase(lResult.begin(),lResult.end());
- // difference 1 2
- Form1->Memo1->Lines->Strings[ct++] = "Difference:" ;
- set_difference (listOne.begin(), listOne.end(),
- listTwo.begin(), listTwo.end(),inserter(lResult, lResult.begin()));
- for(lItr = lResult.begin(); lItr != lResult.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
- ct++;
- lResult.erase(lResult.begin(),lResult.end());
- // symmetric difference 1 2 6 7
- Form1->Memo1->Lines->Strings[ct++] = "Symmetric difference:" ;
- set_symmetric_difference (listOne.begin(), listOne.end(),
- listTwo.begin(), listTwo.end(),inserter(lResult, lResult.begin()));
- for(lItr = lResult.begin(); lItr != lResult.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
- ct++;
-
- if (includes(listOne.begin(), listOne.end(),
- listTwo.begin(), listTwo.end()))
- Form1->Memo1->Lines->Strings[ct++] = "set is subset" ;
- else
- Form1->Memo1->Lines->Strings[ct++] = "set is not subset" ;
-
- }
-
-
- void heap_example ()
- // illustrate the use of the heap functions
- {
- // make a heap of 15 random integers
- vector<int> aVec(15);
- vector<int>::iterator lItr;
- generate (aVec.begin(), aVec.end(), randomValue);
- make_heap (aVec.begin(), aVec.end());
- Form1->Memo1->Lines->Strings[ct++] = "Heap:" ;
- for(lItr = aVec.begin(); lItr != aVec.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
- ct++;
-
- Form1->Memo1->Lines->Strings[ct++] = "Largest value " + IntToStr(aVec.front() );
-
- // remove largest and reheap
- Form1->Memo1->Lines->Strings[ct++] = "Remove largest:" ;
- pop_heap(aVec.begin(), aVec.end());
- aVec.pop_back();
- for(lItr = aVec.begin(); lItr != aVec.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
- ct++;
-
- // add a 97 to the heap
- aVec.push_back(97);
- Form1->Memo1->Lines->Strings[ct++] = "Add 97:" ;
- push_heap (aVec.begin(), aVec.end());
- for(lItr = aVec.begin(); lItr != aVec.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
- ct++;
-
- // finally, make into sorted collection
- Form1->Memo1->Lines->Strings[ct++] = "Sort:" ;
- sort_heap (aVec.begin(), aVec.end());
- for(lItr = aVec.begin(); lItr != aVec.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
- ct++;
- }
-
- int alg7_ex() { /* alg7 */
- Form1->Memo1->Lines->Strings[ct++] = "===== STL generic algorithms - Sorting ===";
-
- sortExample();
- partial_sort_example();
- nth_element_example();
- binary_search_example();
- merge_example();
- set_example();
- heap_example();
-
- Form1->Memo1->Lines->Strings[ct++] = "End sorting examples" ;
- return 0;
- }
-
-
- // *** start auto_ptr source
-
- //
- // A simple structure.
- //
-
- struct X
- {
- X (int i = 0) : m_i(i) { }
- int get() const { return m_i; }
- int m_i;
- };
-
- int auto_ptr () /* auto_ptr */
- {
-
- Form1->Memo1->Lines->Strings[ct++] = " =========== Auto pointer Example ========";
- //
- // b will hold a pointer to an X.
- //
- std::auto_ptr<X> b(new X(12345));
- //
- // a will now be the owner of the underlying pointer.
- //
- std::auto_ptr<X> a = b;
- //
- // Output the value contained by the underlying pointer.
- //
- Form1->Memo1->Lines->Strings[ct++] = a->get() ;
- //
- // The pointer will be delete'd when a is destroyed on leaving scope.
- //
- return 0;
- }
-
-
- int binders () /* binders */
- {
- typedef vector<int>::iterator iterator;
- int d1[4] = {1,2,3,4};
- Form1->Memo1->Lines->Strings[ct++] = " ============= Binders Example ===========";
- //
- // Set up a vector.
- //
- vector<int> v1(d1+0, d1+4);
- //
- // Create an 'equal to 3' unary predicate by binding 3 to
- // the equal_to binary predicate.
- //
- binder1st<equal_to<int> > equal_to_3 = bind1st(equal_to<int>(),3);
- //
- // Now use this new predicate in a call to find_if.
- //
- iterator it1 = find_if(v1.begin(),v1.end(),equal_to_3);
- //
- // Even better, construct the new predicate on the fly.
- //
- iterator it2 = find_if(v1.begin(),v1.end(),bind1st(equal_to<int>(),3));
- //
- // And now the same thing using bind2nd.
- // Same result since == is commutative.
- //
- iterator it3 = find_if(v1.begin(),v1.end(),bind2nd(equal_to<int>(),3));
- //
- // Output results.
- //
- Form1->Memo1->Lines->Strings[ct++] = IntToStr(*it1) + " " + IntToStr(*it2) + " " + IntToStr(*it3) ;
-
- return 0;
- }
-
-
- int bitset () /* bitset */
- {
- Form1->Memo1->Lines->Strings[ct++] = " ============= Bitset Example ============";
- std::bitset<8> b;
- Form1->Memo1->Lines->Strings[ct++] = "Initial value: " + (String)(b.to_string()).data() ;
- b |= 5;
- Form1->Memo1->Lines->Strings[ct++] = "After |= 5 operation: " +(String)(b.to_string()).data() ; // results in 00000101
- return 0;
- }
-
-
- int complex1 () /* complex */
- {
-
- Form1->Memo1->Lines->Strings[ct++] = " ============= Complex Example ===========";
-
- complex<double> a(1.2, 3.4);
- complex<double> b(-9.8, -7.6);
-
- a += b;
- a /= sin(b) * cos(a);
- b *= log(a) + pow(b, a);
-
- Form1->Memo1->Lines->Strings[ct++] = "a = (" + FloatToStr((long double)a.real()) +
- "," + FloatToStr((long double)a.imag()) + ")";
- Form1->Memo1->Lines->Strings[ct++] = "b = (" + FloatToStr((long double)b.real()) +
- "," + FloatToStr((long double)b.imag()) + ")" ;
-
- return 0;
- }
-
- // *** start complx source
-
- typedef complex<double> dcomplex;
-
- pair<dcomplex, dcomplex> quadratic
- (dcomplex a, dcomplex b, dcomplex c)
- // return roots of a quadratic equation
- {
- dcomplex root = sqrt(b * b - 4.0 * a * c);
- a = a * 2.0;
-
- return make_pair (
- (-b + root) / a,
- (-b - root) / a);
- }
-
- int complex2() { /* complx */
- dcomplex a(2, 3);
- dcomplex b(4, 5);
- dcomplex c(6, 7);
-
- Form1->Memo1->Lines->Strings[ct++] = " ============ Complex2 Example ===========";
- pair<dcomplex, dcomplex> ans = quadratic(a, b, c);
- Form1->Memo1->Lines->Strings[ct++] = "Roots are: (" + FloatToStr((long double)ans.first.real()) +
- "," + FloatToStr((long double)ans.first.imag()) + ")";
- Form1->Memo1->Lines->Strings[ct++] = " (" + FloatToStr((long double)ans.second.real()) +
- "," + FloatToStr((long double)ans.second.imag()) + ")" ;
- return 0;
- }
-
-
- int copy_ex () /* copyex */
- {
- int d1[4] = {1,2,3,4};
- int d2[4] = {5,6,7,8};
- Form1->Memo1->Lines->Strings[ct++] = " ============== CopyEx Example ===========";
- //
- // Set up three vectors.
- //
- vector<int> v1(d1+0, d1+4), v2(d2+0, d2+4), v3(d2+0, d2+4);
- //
- // Set up one empty vector.
- //
- vector<int> v4;
- //
- // Copy v1 to v2.
- //
- copy(v1.begin(), v1.end(), v2.begin());
- //
- // Copy backwards v1 to v3.
- //
- copy_backward(v1.begin(), v1.end(), v3.end());
- //
- // Use insert iterator to copy into empty vector.
- //
- copy(v1.begin(), v1.end(), back_inserter(v4));
- //
- // Copy all four to cout.
- //
- vector<int>::iterator out;
- for(out = v1.begin(); out != v1.end(); out++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*out) + " ";
- ct++;
-
- return 0;
- }
-
- int count_ex () /*count */
- {
- int sequence[10] = {1,2,3,4,5,5,7,8,9,10};
- int i=0,j=0,k=0;
- Form1->Memo1->Lines->Strings[ct++] = " ============== Count Example ============";
- //
- // Set up a vector.
- //
- vector<int> v(sequence+0, sequence+10);
-
- count(v.begin(),v.end(),5,i); // Count fives
- count(v.begin(),v.end(),6,j); // Count sixes
- //
- // Count all less than 8.
- //
- count_if(v.begin(), v.end(), bind2nd(less<int>(),8),k);
-
- Form1->Memo1->Lines->Strings[ct++] = IntToStr(i) + " " + IntToStr(j) + " " + IntToStr(k) ;
-
- return 0;
- }
-
- // *** start deque source
-
- deque<string> deck_of_cards;
- deque<string> current_hand;
-
- void initialize_cards(deque<string>& cards)
- {
- cards.push_front("aceofspades");
- cards.push_front("kingofspades");
- cards.push_front("queenofspades");
- cards.push_front("jackofspades");
- cards.push_front("tenofspades");
- //
- // etc.
- //
- }
-
- template <class It, class It2>
- void print_current_hand(It start, It2 end)
- {
- while (start < end)
- Form1->Memo1->Lines->Strings[ct++] = (*start++).data() ;
- }
-
-
- template <class It, class It2>
- void deal_cards(It, It2 end)
- {
- for (int i=0;i<5;i++)
- {
- current_hand.insert(current_hand.begin(),*end);
- deck_of_cards.erase(end++);
- }
- }
-
- void play_poker()
- {
- initialize_cards(deck_of_cards);
- deal_cards(current_hand.begin(),deck_of_cards.begin());
- }
-
- int deque_ex () /* deque */
- {
- Form1->Memo1->Lines->Strings[ct++] = " ============= Deque Example =============";
- play_poker();
- print_current_hand(current_hand.begin(),current_hand.end());
- return 0;
- }
-
-
- int distance_ex() /* distance */
- {
- Form1->Memo1->Lines->Strings[ct++] = " ============= Distance Example ==========";
- //
- // Initialize a vector using an array.
- //
- int arr[6] = {3,4,5,6,7,8};
- vector<int> v(arr+0, arr+6);
- //
- // Declare a list iterator, s.b. a ForwardIterator.
- //
- vector<int>::iterator itr;
- //
- // Output the original vector.
- //
- Form1->Memo1->Lines->Strings[ct++] = "For the vector: ";
- for(itr = v.begin(); itr != v.end(); itr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
- ct++;
- itr = v.begin()+3;
- Form1->Memo1->Lines->Strings[ct++] = "When the iterator is initialized to point to " + IntToStr(*itr) ;
- //
- // Use of distance.
- //
- vector<int>::difference_type dist;
- distance(v.begin(), itr, dist);
- Form1->Memo1->Lines->Strings[ct++] = "The distance between the beginning and itr is " + IntToStr(dist) ;
-
- return 0;
- }
-
-
- int eqlrange_ex () /* eqlrange */
- {
- typedef vector<int>::iterator iterator;
- Form1->Memo1->Lines->Strings[ct++] = " =========== Equal Range Example =========";
- int d1[11] = {0,1,2,2,3,4,2,2,2,6,7};
- //
- // Set up a vector.
- //
- vector<int> v1(d1+0, d1+11);
- //
- // Try equal_range variants.
- //
- pair<iterator,iterator> p1 = equal_range(v1.begin(),v1.end(),3);
- pair<iterator,iterator> p2 = equal_range(v1.begin(),v1.end(),2,less<int>());
- //
- // Output results.
- //
- Form1->Memo1->Lines->Strings[ct++] = "The equal range for 3 is: ";
- Form1->Memo1->Lines->Strings[ct++] = "( " + IntToStr(*p1.first)+" " + IntToStr(*p1.second) + " ) " ;
-
- Form1->Memo1->Lines->Strings[ct++] = "The equal range for 2 is: ";
- Form1->Memo1->Lines->Strings[ct++] = "( " + IntToStr(*p2.first) + " "
- + IntToStr(*p2.second) + " ) " ;
-
- return 0;
- }
-
-
- int equal_ex () /* equal */
- {
- int d1[4] = {1,2,3,4};
- int d2[4] = {1,2,4,3};
- Form1->Memo1->Lines->Strings[ct++] = " ============== Equal Example ===========";
- //
- // Set up two vectors.
- //
- vector<int> v1(d1+0, d1+4), v2(d2+0, d2+4);
- //
- // Check for equality.
- //
- bool b1 = equal(v1.begin(), v1.end(), v2.begin());
- bool b2 = equal(v1.begin(), v1.end(), v2.begin(),equal_to<int>());
- //
- // Both b1 and b2 are false.
- //
- Form1->Memo1->Lines->Strings[ct++] = (String)(b1 ? "TRUE" : "FALSE") + " "
- + (String)(b2 ? "TRUE" : "FALSE") ;
- return 0;
- }
-
-
- int fill_ex () /* fill */
- {
- int d1[4] = {1,2,3,4};
- //
- // Set up two vectors.
- //
- Form1->Memo1->Lines->Strings[ct++] = " =============== Fill Example ============";
- vector<int> v1(d1+0, d1+4), v2(d1+0, d1+4);
- //
- // Set up one empty vector.
- //
- vector<int> v3;
- vector<int>::iterator itr;
- //
- // Fill all of v1 with 9.
- //
- fill(v1.begin(), v1.end(), 9);
- //
- // Fill first 3 of v2 with 7.
- //
- fill_n(v2.begin(), 3, 7);
- //
- // Use insert iterator to fill v3 with 5 11's.
- //
- fill_n(back_inserter(v3), 5, 11);
- //
- // Copy all three to cout.
- //
-
- for(itr = v1.begin(); itr != v1.end(); itr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
- ct++;
- for(itr = v2.begin(); itr != v2.end(); itr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
- ct++;
- for(itr = v3.begin(); itr != v3.end(); itr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
- ct++;
-
-
- //
- // Fill cout with 3 5's.
- //
- fill_n(v1.begin(), 3, 5);
- for(itr = v1.begin(); itr != v1.end(); itr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
- ct++;
-
- return 0;
- }
-
-
- int find_ex () /* find */
- {
- typedef vector<int>::iterator iterator;
- Form1->Memo1->Lines->Strings[ct++] = " =============== Find Example ============";
- int d1[10] = {0,1,2,2,3,4,2,2,6,7};
- //
- // Set up a vector.
- //
- vector<int> v1(d1+0, d1+10);
- //
- // Try find.
- //
- iterator it1 = find(v1.begin(), v1.end(), 3);
- //
- // Try find_if.
- //
- iterator it2 = find_if(v1.begin(), v1.end(), bind1st(equal_to<int>(), 3));
- //
- // Try both adjacent_find variants.
- //
- iterator it3 = adjacent_find(v1.begin(), v1.end());
- iterator it4 = adjacent_find(v1.begin(), v1.end(), equal_to<int>());
- //
- // Output results.
- //
- Form1->Memo1->Lines->Strings[ct++] = IntToStr(*it1) + " " + IntToStr(*it2) + " " + IntToStr(*it3) + " " + IntToStr(*it4) ;
-
- return 0;
- }
-
-
- int find_fo_ex() /* find_f_o */
- {
- typedef vector<int>::iterator iterator;
- int d1[10] = {0,1,2,2,3,4,2,2,6,7};
- int d2[2] = {6,4};
- Form1->Memo1->Lines->Strings[ct++] = " ========== Find First Of Example ========";
- //
- // Set up two vectors.
- //
- vector<int> v1(d1+0, d1+10), v2(d2+0, d2+2);
- vector<int>::iterator vItr;
- //
- // Try both find_first_of variants.
- //
- iterator it1 = find_first_of(v1.begin(), v1.end(), v2.begin(), v2.end());
-
- find_first_of(v1.begin(), v1.end(), v2.begin(), v2.end(), equal_to<int>());
- //
- // Output results.
- //
- Form1->Memo1->Lines->Strings[ct++] = "For the vectors: ";
- for(vItr = v1.begin(); vItr != v1.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- Form1->Memo1->Lines->Strings[ct++] = " and ";
- for(vItr = v2.begin(); vItr != v2.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- Form1->Memo1->Lines->Strings[ct++] = "both versions of find_first_of point to: "+IntToStr(*it1);
-
- return 0;
- }
-
- // *** start for_each source
-
- //
- // Function class that outputs its argument times x.
- //
- template <class Arg>
- class out_times_x : private unary_function<Arg,void>
- {
- private:
- Arg multiplier;
- public:
- out_times_x(const Arg& x) : multiplier(x) { }
- void operator()(const Arg& x) {Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr (x * multiplier) + " " ; }
- };
-
- int for_each_ex () /* for_each */
- {
- int sequence[5] = {1,2,3,4,5};
- Form1->Memo1->Lines->Strings[ct++] = " ============= For Each Example =========";
- //
- // Set up a vector.
- //
- vector<int> v(sequence+0, sequence+5);
- vector<int>::iterator itr;
- Form1->Memo1->Lines->Strings[ct++] = " Original vector:";
- for(itr = v.begin(); itr != v.end(); itr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
- ct++;
- //
- // Setup a function object.
- //
- out_times_x<int> f2(2);
- Form1->Memo1->Lines->Strings[ct++] = " After times-two operation:";
-
- for_each(v.begin(),v.end(),f2); // Apply function
- ct++;
-
- return 0;
- }
-
-
- //
- // Create a new function object from unary_function.
- //
- template<class Arg>
- class factorial : public unary_function<Arg, Arg>
- {
- public:
- Arg operator() (const Arg& arg);
- };
-
- template<class Arg>
- Arg factorial<Arg>::operator() (const Arg& arg)
- {
- Arg a = 1;
- for (Arg i = 2; i <= arg; i++)
- a *= i;
- return a;
- }
-
- int funct_ob_ex () /* funct_ob*/
- {
- Form1->Memo1->Lines->Strings[ct++] = " ============= Funct_ob Example =========";
- //
- // Initialize a deque with an array of integers.
- //
- int init[7] = {1,2,3,4,5,6,7};
- deque<int> d(init+0, init+7);
- deque<int>::iterator dItr;
- //
- // Create an empty vector to store the factorials.
- //
- vector<int> v((size_t)7);
- vector<int>::iterator itr;
- //
- // Transform the numbers in the deque to their factorials and store
- // in the vector.
- //
- transform(d.begin(), d.end(), v.begin(), factorial<int>());
- //
- // Print the results.
- //
- Form1->Memo1->Lines->Strings[ct++] = "The following numbers: " ;
- for(dItr = d.begin(); dItr != d.end(); dItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*dItr) + " ";
- ct++;
-
- Form1->Memo1->Lines->Strings[ct++] = "Have the factorials: " ;
- for(itr = v.begin(); itr != v.end(); itr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
- ct++;
-
- return 0;
- }
-
-
-
-
- int heap_opts_ex () /* heap_opts */
- {
- int d1[4] = {1,2,3,4};
- int d2[4] = {1,3,2,4};
- Form1->Memo1->Lines->Strings[ct++] = " ============== Heap Example ===========";
- //
- // Set up two vectors.
- //
- vector<int> v1(d1+0, d1+4), v2(d2+0, d2+4);
- vector<int> vResult;
- vector<int>::iterator vItr;
- //
- // Make heaps.
- //
- make_heap(v1.begin(), v1.end());
- make_heap(v2.begin(), v2.end(), less<int>());
- //
- // v1 = (4,x,y,z) and v2 = (4,x,y,z)
- //
- // Note that x, y and z represent the remaining values in the
- // container (other than 4). The definition of the heap and heap
- // operations does not require any particular ordering
- // of these values.
- //
- // Copy both vectors to cout.
- //
- for(vItr = v1.begin(); vItr != v1.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- for(vItr = v2.begin(); vItr != v2.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- //
- // Now let's pop.
- //
- pop_heap(v1.begin(), v1.end());
- pop_heap(v2.begin(), v2.end(), less<int>());
- //
- // Copy both vectors to cout.
- //
- for(vItr = v1.begin(); vItr != v1.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- for(vItr = v2.begin(); vItr != v2.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- //
- // And push.
- //
- push_heap(v1.begin(), v1.end());
- push_heap(v2.begin(), v2.end(), less<int>());
- //
- // Copy both vectors to cout.
- //
- for(vItr = v1.begin(); vItr != v1.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- for(vItr = v2.begin(); vItr != v2.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- //
- // Now sort those heaps.
- //
- sort_heap(v1.begin(), v1.end());
- sort_heap(v2.begin(), v2.end(), less<int>());
- //
- // Copy both vectors to cout.
- //
- for(vItr = v1.begin(); vItr != v1.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- for(vItr = v2.begin(); vItr != v2.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
-
- return 0;
- }
-
- // Begin ice cream simulation example source
-
- // class event
- // execution event in a descrete event driven simulation
- //
-
- class event {
- public:
- // construct sets time of event
- event (unsigned int t) : time(t) { };
-
- // time is a public data field
- const unsigned int time;
-
- // execute event my invoking this method
- virtual void processEvent() = 0;
- };
-
- inline void destroy(event **) {}
-
- struct eventComparison {
- bool operator () (event * left, event * right)
- { return left->time > right->time; }
- };
-
- //
- // class simulation
- // framework for discrete event-driven simulations
- // written by Tim Budd, for Rogue Wave, 1995
- //
-
- class simulation {
- public:
- // constructor
- simulation () : eventQueue(), time(0) {}
-
- // start and run simulation
- void run ();
-
- // return current time
- unsigned int time;
-
- // schedule a future event
- void scheduleEvent (event * newEvent)
- { eventQueue.push(newEvent); }
-
- protected:
- priority_queue<event*, vector<event *>, eventComparison> eventQueue;
- };
-
- void simulation::run()
- {
- while (! eventQueue.empty()) {
- event * nextEvent = eventQueue.top();
- eventQueue.pop();
- time = nextEvent->time;
- nextEvent->processEvent();
- delete nextEvent;
- }
- }
-
- //
- // ice cream store simulation
- //
-
- class storeSimulation : public simulation {
- public:
- storeSimulation()
- : freeChairs(35), profit(0.0), simulation() { }
-
- bool canSeat(unsigned int numberOfPeople);
- void order(unsigned int numberOfScoops);
- void leave(unsigned int numberOfPeople);
-
- // data fields
- unsigned int freeChairs;
- double profit;
- } theSimulation;
-
- class arriveEvent : public event {
- public:
- arriveEvent (unsigned int time, unsigned int groupSize)
- : event(time), size(groupSize) { }
- virtual void processEvent();
- private:
- unsigned int size;
- };
-
- class orderEvent : public event {
- public:
- orderEvent (unsigned int time, unsigned int groupSize)
- : event(time), size(groupSize) { }
- virtual void processEvent();
- private:
- unsigned int size;
- };
-
- class leaveEvent : public event {
- public:
- leaveEvent (unsigned int time, unsigned int groupSize)
- : event(time), size(groupSize) { }
- virtual void processEvent();
- private:
- unsigned int size;
- };
-
- int irand(int n)
- // return random integer between 0 and n
- { return (rand()/10) % n; }
-
- void arriveEvent::processEvent()
- {
- if (theSimulation.canSeat(size))
- theSimulation.scheduleEvent(new orderEvent(time + 1 + irand(4), size));
- }
-
- void orderEvent::processEvent()
- {
- // each person orders some number of scoops
- for (int i = 0; i < size; i++)
- theSimulation.order(1 + irand(4));
- // then we schedule the leave event
- theSimulation.scheduleEvent(new leaveEvent(time + 1 + irand(10), size));
- }
-
- void leaveEvent::processEvent()
- {
- theSimulation.leave(size);
- }
-
- bool storeSimulation::canSeat(unsigned int numberOfPeople)
- // if sufficient room then seat customers
- {
- Form1->Memo1->Lines->Strings[ct++] = "Time: " + IntToStr(time) +
- " group of " + IntToStr(numberOfPeople) + " customers arrives";
- if (numberOfPeople < freeChairs) {
- Form1->Memo1->Lines->Strings[ct++] = " is seated" ;
- freeChairs -= numberOfPeople;
- return true;
- }
- else {
- Form1->Memo1->Lines->Strings[ct++] = " no room, they leave";
- return false;
- }
- }
-
- void storeSimulation::order(unsigned int numberOfScoops)
- // service icecream, compute profits
- {
- Form1->Memo1->Lines->Strings[ct++] = "Time: " + IntToStr(time) +
- " serviced order for " + IntToStr(numberOfScoops) ;
- profit += 0.35 * numberOfScoops;
- }
-
- void storeSimulation::leave(unsigned int numberOfPeople)
- // people leave, free up chairs
- {
- Form1->Memo1->Lines->Strings[ct++] = "Time: " + IntToStr(time) +
- " group of size " + IntToStr(numberOfPeople) + " leaves" ;
- freeChairs += numberOfPeople;
- }
-
- int ice_cream_ex() {
- Form1->Memo1->Lines->Strings[ct++] = "============ Ice Cream Store simulation from Chapter 9";
- // load queue with some number of initial events
- unsigned int t = 0;
- while (t < 20) {
- t += irand(6);
- Form1->Memo1->Lines->Strings[ct++] = "pumping queue with event " + IntToStr(t);
- theSimulation.scheduleEvent(new arriveEvent(t, 1 + irand(4)));
- }
-
- // run the simulation
- theSimulation.run();
- Form1->Memo1->Lines->Strings[ct++] = "Total profits " + FloatToStr(theSimulation.profit);
-
- Form1->Memo1->Lines->Strings[ct++] = "End of ice cream store simulation" ;
- return 0;
- }
-
-
- int innerprod_ex ()
- {
- //
- // Initialize a list and an int using arrays of ints.
- //
- int a1[3] = {6, -3, -2};
- int a2[3] = {-2, -3, -2};
-
- list<int> l(a1+0, a1+3);
- vector<int> v(a2+0, a2+3);
- list<int>::iterator lItr;
- vector<int>::iterator vItr;
- Form1->Memo1->Lines->Strings[ct++] = " =========== Inner Product Example ========";
- //
- // Calculate the inner product of the two sets of values.
- //
- int inner_prod = inner_product(l.begin(), l.end(), v.begin(), 0);
- //
- // Calculate a wacky inner product using the same values.
- //
- int wacky = inner_product(l.begin(), l.end(), v.begin(), 0,
- plus<int>(), minus<int>());
- //
- // Print the output.
- //
- Form1->Memo1->Lines->Strings[ct++] = "For the two sets of numbers: " ;
- for(vItr = v.begin(); vItr != v.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- Form1->Memo1->Lines->Strings[ct++] = " and ";
- for(lItr = l.begin(); lItr != l.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
- ct++;
-
- Form1->Memo1->Lines->Strings[ct] = Form1->Memo1->Lines->Strings[ct]+ "," ;
- Form1->Memo1->Lines->Strings[ct++] = "The inner product is: " + IntToStr(inner_prod) ;
- Form1->Memo1->Lines->Strings[ct++] = "The wacky result is: " + IntToStr(wacky);
-
- return 0;
- }
-
-
- int lex_comp_ex(void)
- {
- Form1->Memo1->Lines->Strings[ct++] = " ======== Lexicographic compare Example ========";
- int d1[5] = {1,3,5,32,64};
- int d2[5] = {1,3,2,43,56};
- //
- // Set up vector.
- //
- vector<int> v1(d1+0, d1+5), v2(d2+0, d2+5);
- //
- // Is v1 less than v2 (I think not).
- //
- bool b1 = lexicographical_compare(v1.begin(),v1.end(),v2.begin(),v2.end());
- //
- // Is v2 less than v1 (yup, sure is).
- //
- bool b2 = lexicographical_compare(v2.begin(), v2.end(),
- v1.begin(), v1.end(), less<int>());
- Form1->Memo1->Lines->Strings[ct++] = (String)(b1 ? "TRUE" : "FALSE") + " "
- + (String)(b2 ? "TRUE" : "FALSE") ;
-
- return 0;
- }
-
- int list_ex ()
- {
- //
- // Create a list of critters.
- //
- list<string> critters;
- list<string>::iterator lItr;
- int i;
- Form1->Memo1->Lines->Strings[ct++] = " ========== List Example ========";
- //
- // Insert several critters.
- //
- critters.insert(critters.begin(),"antelope");
- critters.insert(critters.begin(),"bear");
- critters.insert(critters.begin(),"cat");
- //
- // Print out the list.
- //
- for(lItr = critters.begin(); lItr != critters.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + (String)(*lItr).data() + " ";
- ct++;
- //
- // Change cat to cougar.
- //
- *find(critters.begin(),critters.end(),"cat") = "cougar";
- for(lItr = critters.begin(); lItr != critters.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + (String)(*lItr).data() + " ";
- ct++;
- //
- // Put a zebra at the beginning, an ocelot ahead of antelope,
- // and a rat at the end.
- //
- critters.push_front("zebra");
- critters.insert(find(critters.begin(),critters.end(),"antelope"),"ocelot");
- critters.push_back("rat");
- for(lItr = critters.begin(); lItr != critters.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + (String)(*lItr).data() + " ";
- ct++;
- //
- // Sort the list (Use list's sort function since the
- // generic algorithm requires a random access iterator
- // and list only provides bidirectional)
- //
- critters.sort();
- for(lItr = critters.begin(); lItr != critters.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + (String)(*lItr).data() + " ";
- ct++;
- //
- // Now let's erase half of the critters.
- //
- int half = critters.size() / 2;
- for (i = 0; i < half; ++i)
- critters.erase(critters.begin());
- for(lItr = critters.begin(); lItr != critters.end(); lItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + (String)(*lItr).data() + " ";
- ct++;
-
- return 0;
- }
-
-
-
- int merge_ex ()
- {
- int d1[4] = {1,2,3,4};
- int d2[8] = {11,13,15,17,12,14,16,18};
- //
- // Set up two vectors.
- //
- Form1->Memo1->Lines->Strings[ct++] = " ============ Merge Example ========";
- vector<int> v1(d1+0, d1+4), v2(d1+0, d1+4);
- //
- // Set up four destination vectors.
- //
- vector<int> v3(d2+0, d2+8), v4(d2+0, d2+8), v5(d2+0, d2+8), v6(d2+0, d2+8);
- //
- // Set up one empty vector.
- //
- vector<int> v7;
- vector<int> vResult;
- vector<int>::iterator vItr;
- //
- // Merge v1 with v2.
- //
- merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
- //
- // Now use comparator.
- //
- merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v4.begin(), less<int>());
- //
- // In place merge v5.
- //
- vector<int>::iterator mid = v5.begin();
- advance(mid,4);
- inplace_merge(v5.begin(),mid,v5.end());
- //
- // Now use a comparator on v6.
- //
- mid = v6.begin();
- advance(mid,4);
- inplace_merge(v6.begin(), mid, v6.end(), less<int>());
- //
- // Merge v1 and v2 to empty vector using insert iterator.
- //
- merge(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(v7));
-
- for(vItr = v1.begin(); vItr != v1.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- for(vItr = v2.begin(); vItr != v2.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- for(vItr = v3.begin(); vItr != v3.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- for(vItr = v4.begin(); vItr != v4.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- for(vItr = v5.begin(); vItr != v5.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- for(vItr = v6.begin(); vItr != v6.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- for(vItr = v7.begin(); vItr != v7.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
- //
- // Merge v1 and v2 .
- //
- merge(v1.begin(),v1.end(),v2.begin(),v2.end(),
- inserter(vResult, vResult.begin()));
- for(vItr = vResult.begin(); vItr != vResult.end(); vItr++)
- Form1->Memo1->Lines->Strings[ct] =
- Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
- ct++;
-
- return 0;
- }
-
-
- int pqueue_ex ()
- {
- //
- // Make a priority queue of int using a deque container.
- //
- priority_queue<int, vector<int>, less<int> > pq;
- //
- // Push a couple of values.
- //
- Form1->Memo1->Lines->Strings[ct++] = " ========= Priority Queue Example =====";
- pq.push(1);
- pq.push(2);
- //
- // Pop a couple of values and examine the ends.
- //
- Form1->Memo1->Lines->Strings[ct++] = IntToStr(pq.top()) ;
- pq.pop();
- Form1->Memo1->Lines->Strings[ct++] = IntToStr(pq.top());
- pq.pop();
- //
- // Make a priority queue of strings.
- //
- priority_queue<string,deque<string>, less<string> > pqs;
- //
- // Push on a few strings then pop them back off.
- //
- int i;
- for (i = 0; i < 10; i++)
- {
- pqs.push(string(i+1,'a'));
- Form1->Memo1->Lines->Strings[ct++] = (String)(pqs.top()).data() ;
- }
- for (i = 0; i < 10; i++)
- {
- Form1->Memo1->Lines->Strings[ct++] = (String)(pqs.top()).data() ;
- pqs.pop();
- }
- //
- // Make a priority queue of strings using greater.
- //
- priority_queue<string,deque<string>, greater<string> > pgqs;
- //
- // Push on a few strings then pop them back off.
- //
- for (i = 0; i < 10; i++)
- {
- pgqs.push(string(i+1,'a'));
- Form1->Memo1->Lines->Strings[ct++] = (String)(pgqs.top()).data() ;
- }
- for (i = 0; i < 10; i++)
- {
- Form1->Memo1->Lines->Strings[ct++] = (String)(pgqs.top()).data() ;
- pgqs.pop();
- }
-
- return 0;
- }
-
- int rev_itr_ex()
- {
- //
- // Initialize a vector using an array.
- //
- int arr[4] = {3,4,7,8};
- vector<int> v(arr+0, arr+4);
- //
- // Output the original vector.
- //
- Form1->Memo1->Lines->Strings[ct++] = " ========= Reverse Iterator Example =====";
- Form1->Memo1->Lines->Strings[ct++] = "Traversing vector with iterator:" ;
- for(vector<int>::iterator i = v.begin(); i != v.end(); i++)
- Form1->Memo1->Lines->Strings[ct] = Form1->Memo1->Lines->Strings[ct] + IntToStr(*i) + " ";
- ct++;
- //
- // Declare the reverse_iterator.
- //
- vector<int>::reverse_iterator rev(v.end());
- vector<int>::reverse_iterator rev_end(v.begin());
- //
- // Output the vector backwards.
- //
- Form1->Memo1->Lines->Strings[ct++] = "Same vector, same loop, reverse_iterator: " ;
- for(; rev != rev_end; rev++)
- Form1->Memo1->Lines->Strings[ct] = Form1->Memo1->Lines->Strings[ct] + IntToStr(*rev) + " ";
- ct++;
- return 0;
- }
-
-
- int sieve_ex() {
- Form1->Memo1->Lines->Strings[ct++] = "======= Prime Sieve example, using vectors ==";
-
- // create a sieve of bits, initially on
- const int sievesize = 100;
- vector<int> sieve(sievesize, 1);
-
- // now search for 1 bt positions
- for (int i = 2; i * i < sievesize; i++)
- if (sieve[i])
- for (int j = i + i; j < sievesize; j += i)
- sieve[j] = 0;
-
- // now output all the values that are set
- for (int j = 2; j < sievesize; j++)
- if (sieve[j])
- Form1->Memo1->Lines->Strings[ct] = Form1->Memo1->Lines->Strings[ct] + IntToStr(j) + " ";
- ct++;
-
- Form1->Memo1->Lines->Strings[ct++] = "End of Prime Sieve program" ;
- return 0;
- }
-
-
- int stack_ex ()
- {
- //
- // Make a stack using a vector container.
- //
- stack<int,vector<int> > s;
- //
- // Push a couple of values on the stack.
- //
- Form1->Memo1->Lines->Strings[ct++] = "============= Stack example ========";
- s.push(1);
- s.push(2);
- Form1->Memo1->Lines->Strings[ct++] = IntToStr(s.top()) ;
- //
- // Now pop them off.
- //
- s.pop();
- Form1->Memo1->Lines->Strings[ct++] = IntToStr(s.top());
- s.pop();
- //
- // Make a stack of strings using a deque.
- //
- stack<string,deque<string> > ss;
- //
- // Push a bunch of strings on then pop them off.
- //
- int i;
- for (i = 0; i < 10; i++)
- {
- ss.push(string(i+1,'a'));
- Form1->Memo1->Lines->Strings[ct++] = (String)(ss.top()).data() ;
- }
- for (i = 0; i < 10; i++)
- {
- Form1->Memo1->Lines->Strings[ct++] = (String)(ss.top()).data() ;
- ss.pop();
- }
-
- return 0;
- }
-
- // begin telephone example source
-
- typedef map<string, long, less<string> > friendMap;
- typedef map<long, string, less<long> > sortedMap;
-
- //
- // utility functions used in telephone directory
- //
-
- typedef friendMap::value_type entry_type;
- typedef sortedMap::value_type sorted_entry_type;
-
- void printEntry(const entry_type & entry)
- { Form1->Memo1->Lines->Strings[ct++] = (String)entry.first.data() + ":" + IntToStr(entry.second) ; }
-
- void printSortedEntry(const sorted_entry_type & entry)
- { Form1->Memo1->Lines->Strings[ct++] = IntToStr(entry.first) + ":" + (String)entry.second.data(); }
-
- int prefix(const entry_type& entry)
- { return entry.second / 10000; }
-
- bool prefixCompare(const entry_type & a, const entry_type & b)
- { return prefix(a) < prefix(b); }
-
- class checkPrefix {
- public:
- checkPrefix (int p) : testPrefix(p) { }
- int testPrefix;
- bool operator () (const entry_type& entry)
- { return prefix(entry) == testPrefix; }
- };
-
- class telephoneDirectory {
- public:
- void addEntry (string name, long number)
- { database[name] = number; }
-
- void remove (string name)
- { database.erase(name); }
-
- void update (string name, long number)
- { remove(name); addEntry(name, number); }
-
- void displayDatabase()
- { for_each(database.begin(), database.end(), printEntry); }
-
- void displayPrefix(int);
-
- void displayByPrefix();
-
- private:
- friendMap database;
- };
-
- void telephoneDirectory::displayPrefix(int prefix)
- {
- Form1->Memo1->Lines->Strings[ct++] = "Listing for prefix " + IntToStr(prefix) ;
- map<string, long, less<string> >::iterator where;
- where = find_if(database.begin(), database.end(), checkPrefix(prefix));
- while (where != database.end()) {
- printEntry(*where);
- where = find_if(++where, database.end(), checkPrefix(prefix));
- }
- Form1->Memo1->Lines->Strings[ct++] = "end of prefix listing" ;
- }
-
- void telephoneDirectory::displayByPrefix()
- {
- Form1->Memo1->Lines->Strings[ct++] = "Display by prefix" ;
-
- sortedMap sortedData;
-
- for (friendMap::iterator i = database.begin(); i != database.end(); i++)
- sortedData.insert(sortedMap::value_type((*i).second,
- (*i).first));
-
- for_each(sortedData.begin(), sortedData.end(), printSortedEntry);
- Form1->Memo1->Lines->Strings[ct++] = "end display by prefix" ;
- }
-
- int telephone_ex() {
- Form1->Memo1->Lines->Strings[ct++] = "==== Telephone Directory sample program" ;
- telephoneDirectory friends;
-
- friends.addEntry("Samantha", 6342343);
- friends.addEntry("Brenda", 5436546);
- friends.addEntry("Fred", 7435423);
- friends.addEntry("Allen", 6348723);
-
- friends.displayDatabase();
- friends.displayPrefix(634);
- friends.displayByPrefix();
-
- Form1->Memo1->Lines->Strings[ct++] = "End of telephone directory sample program" ;
- return 0;
- }
-